package com.a0soft.gphone.aTruffleHog.widget;
/*
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 3, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * Copyright(C) 2011 
 *   Sam Lu <ysl.sam.lu (at) gmail.com>
 *
 */

import android.content.Context;
import android.util.AttributeSet;
import android.view.accessibility.AccessibilityEvent;
import android.widget.Checkable;
import android.widget.LinearLayout;

public class CheckableLinearLayout extends LinearLayout 
                                   implements Checkable {
    private boolean m_bChecked = false;
    private static final int[] CHECKED_STATE_SET = {
        android.R.attr.state_checked
    };


    public 
    CheckableLinearLayout(Context context) 
    {
        super(context, null);
    }

    public 
    CheckableLinearLayout(Context context, AttributeSet attrs) 
    {
        super(context, attrs);       
    }

    public void 
    setChecked(boolean bChecked) 
    {
        if (m_bChecked != bChecked) {
            m_bChecked = bChecked;
            refreshDrawableState();
        }
    }

    public boolean 
    isChecked() 
    {
        return m_bChecked;
    }

    public void 
    toggle() 
    {
        setChecked(!m_bChecked);
    }

    @Override protected int[] 
    onCreateDrawableState(int extraSpace) 
    {
        final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
        if (isChecked()) {
            mergeDrawableStates(drawableState, CHECKED_STATE_SET);
        }
        return drawableState;
    }

    /* added for multi-choice
    @Override public boolean 
    performClick() 
    {
        toggle();
        return super.performClick();
    }*/
    
    @Override public boolean 
    dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
        boolean populated = super.dispatchPopulateAccessibilityEvent(event);
        if (!populated) {
            event.setChecked(m_bChecked);
        }
        return populated;
    }
}